<template>
  <div class="main-border">
    <el-menu default-active="1" class="el-menu-demo" mode="horizontal" @select="handleSelect">
      <el-menu-item index="1">上线的视频</el-menu-item>
      <el-menu-item index="2">下架的视频</el-menu-item>
      <el-button type="text" @click="openAdd()">发布视频</el-button>
    </el-menu>

    <el-table v-if="this.mode == 1"
              :data="onlineSciences"
              stripe
              style="width: 100%;color: #5a5c61;">
      <el-table-column
          prop="releaseTime"
          label="发布日期"
          width="200">
      </el-table-column>
      <el-table-column
          prop="scienceName"
          label="茶视频名称"
          show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
          prop="user.nickname"
          label="发布用户"
          show-overflow-tooltip
          min-width="100"
          width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="openEdit(scope.$index)"> 修改视频
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="makeOfflineScience(scope.$index)"> 违规下架
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-table v-show="this.mode == 2"
              :data="OfflineSciences"
              stripe
              style="width: 100%;color: #5a5c61;">
      <el-table-column
          prop="releaseTime"
          label="发布日期"
          width="200">
      </el-table-column>
      <el-table-column
          prop="scienceName"
          label="茶视频名称"
          show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
          prop="user.nickname"
          label="发布用户"
          show-overflow-tooltip
          min-width="100"
          width="100">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="resUpScience(scope.$index)">重新上架
          </el-button>
          <el-button
              size="mini"
              type="danger"
              @click="deleteScience(scope.$index)">永久删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="nowPage"
          :page-size="8"
          background
          layout="prev, pager, next,jumper"
          :total="total">
      </el-pagination>
    </div>

    <!--  新增视频弹窗  -->
    <el-dialog
        title="发布视频"
        :visible.sync="dialogVisible"
        width="60%">
      <el-form class="release-tea-container-form">
        <div class="release-tip">视频名称</div>
        <el-input placeholder="请输入视频名称" v-model="scienceItemInfo.scienceName"
                  maxlength="30"
                  show-word-limit>
        </el-input>
        <div class="release-tip">视频标题</div>
        <el-input
            class="release-tea-detiles-text"
            type="textarea"
            autosize
            placeholder="请输入视频的标题介绍..."
            v-model="scienceItemInfo.scienceTitle"
            maxlength="1000"
            show-word-limit>
        </el-input>
        <div v-show="!scienceItemInfo.scienceId" style="display: flex; justify-content: space-between;">
          <div>
            <div class="release-tip">视频类别</div>
            <el-select v-model="scienceItemInfo.scienceType" placeholder="请选择类别">
              <el-option
                  v-for="item in options2"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
        <div v-show="!scienceItemInfo.scienceId" class="release-tea-container-picture">
          <div class="release-tea-container-picture-title">上传视频</div>
          <el-upload
              action="http://localhost:9321/uploadVideo/"
              :on-preview="fileHandlePreview"
              :on-remove="fileHandleRemove"
              :on-success="handleVideoSuccess"
              :show-file-list="showFileList"
              accept="video/*"
              drag
              multiple>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将视频拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </div>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleClose">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import options from "@/components/common/country-data";

export default {
  name: "teaScience",
  data() {
    return {
      mode: 1,
      nowPage: 1,
      total: 0,
      onlineSciences: [],
      OfflineSciences: [],
      dialogVisible: false,
      scienceItemInfo: {
        scienceName: '',
        scienceUrl: '',
        scienceImg: '',
        scienceType: '',
        scienceTitle: '',
      },
      imgDialogVisible: false,
      dialogImageUrl: '',
      showFileList: true,
      options: options,
      selectedOptions: [],
      options2: [
        {label: '茶道', value: 998},
        {label: '茶史', value: 999},
      ],
      imgList: [],
      videoForm: {videoId: '', videoUrl: ''},
      videoFlag: false,
      videoUploadPercent: 0
    }
  },
  created() {
    this.getOnlineSciences();
  },
  methods: {
    handleCurrentChange(val) {
      this.nowPage = val;
      if (this.mode == 1) {
        this.getOnlineGoods();
      }
      if (this.mode == 2) {
        this.getOfflineGoods();
      }
    },
    handleSelect(val) {
      if (this.mode !== val) {
        this.mode = val;
        if (val == 1) {
          this.nowPage = 1;
          this.getOnlineSciences();
        }
        if (val == 2) {
          this.nowPage = 1;
          this.getOfflineSciences();
        }
      }
    },
    makeOfflineScience(i) {
      this.$api.updateSciences({
        id: this.onlineSciences[i].scienceId,
        status: 2
      }).then(res => {
        if (res.status_code == 1) {
          this.getOnlineSciences();
        } else {
          this.$message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    },
    deleteScience(i) {
      this.$api.updateSciences({
        id: this.OfflineSciences[i].scienceId,
        status: 0
      }).then(res => {
        if (res.status_code == 1) {
          this.getOfflineSciences();
        } else {
          this.$message.error(res.msg)
        }

      }).catch(e => {
        console.log(e)
      })
    },
    // 重新上架
    resUpScience(i) {
      this.$api.updateSciences({
        id: this.OfflineSciences[i].scienceId,
        status: 1
      }).then(res => {
        if (res.status_code == 1) {
          this.getOfflineGoods();
        } else {
          this.$message.error(res.msg)
        }

      }).catch(e => {
        console.log(e)
      })
    },
    getOnlineSciences() {
      this.$api.getscienceItem({
        status: 1,
        page: this.nowPage,
        nums: 8
      }).then(res => {
        console.log(res)
        if (res.status_code == 1) {
          this.onlineSciences = res.data.list;
          this.total = res.data.count;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    },
    getOfflineSciences() {
      this.$api.getscienceItem({
        status: 2,
        page: this.nowPage,
        nums: 8
      }).then(res => {
        if (res.status_code == 1) {
          this.OfflineSciences = res.data.list;
          this.total = res.data.count;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(e => {
        console.log(e)
      })
    },
    handleClose() {
      console.log(this.scienceItemInfo);
      if (this.scienceItemInfo.scienceName &&
          this.scienceItemInfo.scienceUrl &&
          this.scienceItemInfo.scienceType &&
          this.scienceItemInfo.scienceTitle) {
        if (!this.scienceItemInfo.scienceId) {
          this.$api.addscienceItem(this.scienceItemInfo).then(res => {
            if (res.status_code === 1) {
              this.$message({
                message: '发布成功！',
                type: 'success'
              });
              console.log(res.data);
              this.dialogVisible = false;
              this.scienceItemInfo = {
                scienceName: '',
                scienceUrl: '',
                scienceImg: '',
                scienceType: '',
                scienceTitle: '',
              }
              this.getOnlineSciences();
            } else {
              this.$message.error('发布失败！' + res.msg);
            }
          }).catch(e => {
            this.$message.error('请填写完整信息！');
          })
        } else {
          this.$api.updateScienceItem(this.scienceItemInfo).then(res => {
            if (res.status_code === 1) {
              this.$message({
                message: '修改成功！',
                type: 'success'
              });
              console.log(res.data);
              this.dialogVisible = false;
              this.scienceItemInfo = {
                scienceName: '',
                scienceUrl: '',
                scienceImg: '',
                scienceType: '',
                scienceTitle: '',
              }
              this.getOnlineSciences();
            } else {
              this.$message.error('修改失败！' + res.msg);
            }
          })
        }
      }
    },

    fileHandleRemove(file, fileList) {
      console.log(file, fileList);
      for (let i = 0; i < this.imgList.length; i++) {
        if (this.imgList[i] === file.response.data) {
          this.imgList.splice(i, 1);
        }
      }
    },
    fileHandlePreview(file) {
      console.log(file);
      this.dialogImageUrl = file.response.data;
      this.imgDialogVisible = true;
    },
    handleVideoSuccess(res, file) {
      this.videoUploadPercent = 100;
      if (res.resCode === '200') {
        console.log(res)
        this.scienceItemInfo.scienceUrl = res.VideoUrl;
        this.$message.success('视频上传成功！');
      } else {
        this.$message.error('视频上传失败，请重新上传！');
      }
    },
    openAdd() {
      this.dialogVisible = true;
      this.title = "发布物品";
      this.scienceItemInfo = {};
    },
    //   打开修改窗口
    openEdit(i) {
      console.log(this.onlineSciences[i])
      this.dialogVisible = true;
      this.title = "修改物品";
      this.$api.getscience({
        id: this.onlineSciences[i].scienceId,
      }).then(res => {
        console.log(res.data);
        this.scienceItemInfo = res.data;
        console.log(this.scienceItemInfo)
      })
    }
  }
  ,
  mounted() {
    // this.listType()
  }

}
</script>

<style scoped>

.main-border {
  background-color: #FFF;
  padding: 10px 30px;
  box-shadow: 0 1px 15px -6px rgba(0, 0, 0, .5);
  border-radius: 5px;
}

.block {
  display: flex;
  justify-content: center;
  padding-top: 15px;
  padding-bottom: 10px;
  width: 100%;
}

.release-tea-container-form {
  padding: 0 180px;
}

.release-tea-detiles-text {
  margin: 20px 0;
}

.release-tea-place {
  margin-bottom: 15px;
}

.release-tip {
  color: #555555;
  float: left;
  padding-right: 5px;
  height: 36px;
  line-height: 36px;
  font-size: 14px;
}

.release-tea-container-picture {
  width: 500px;
  height: 400px;
  margin: 20px 0;

}

.release-tea-container-picture-title {
  margin: 10px 0;
  color: #555555;
  font-size: 14px;
}

.picture-list {
  margin: 20px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100px;
}
</style>